package cn.wolfcode.service.impl;

import cn.hutool.core.date.DateUtil;
import cn.wolfcode.domain.Employee;
import cn.wolfcode.domain.Notice;
import cn.wolfcode.domain.UnreadNotice;
import cn.wolfcode.mapper.NoticeMapper;
import cn.wolfcode.qo.QueryObject;
import cn.wolfcode.service.IEmployeeService;
import cn.wolfcode.service.INoticeService;
import cn.wolfcode.util.UserContext;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class NoticeServiceImpl implements INoticeService {

    @Autowired
    private NoticeMapper noticeMapper;


    @Autowired
    private IEmployeeService employeeService;

    @Override
    public void save(Notice notice) {
        //获取登录对象
        Employee employee = UserContext.getCurrentUser();
        //设置公告是谁发布的
        notice.setEmployee(employee);
        notice.setCreateTime(DateUtil.date(System.currentTimeMillis()));
        //插入公告表
        noticeMapper.insert(notice);
        Long nid = notice.getId();
        //查询出所有员工
        List<Employee> employees = employeeService.listAll();


        for (Employee e : employees) {
            noticeMapper.insertReadStatus(nid, e.getId(), false);
        }
        Long empId = UserContext.getCurrentUser().getId();

        UnreadNotice unreadNotice = new UnreadNotice();
        //查询出所有未读的公告,设置进去对象里面
        unreadNotice.setNotices(noticeMapper.queryUnreadNotices(empId));
        //查询当前用户未读公告的条数,设置进去对象里面
        unreadNotice.setSize(noticeMapper.queryUnreadSize(empId));
        //设置未读的公告 , 把封装好的数据放到session中去
        UserContext.setUnreadNotice(unreadNotice);
    }

    @Override
    public void delete(Long id) {
        noticeMapper.deleteByPrimaryKey(id);
    }

    @Override
    public void update(Notice notice) {
        notice.setCreateTime(new Date());
        noticeMapper.updateByPrimaryKey(notice);
    }

    @Override
    public Notice get(Long id) {
        return noticeMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<Notice> listAll() {
        return noticeMapper.selectAll();
    }

    @Override
    public PageInfo<Notice> query(Long empId, QueryObject qo) {
        boolean admin = UserContext.getCurrentUser().isAdmin();
        PageHelper.startPage(qo.getCurrentPage(), qo.getPageSize());//对下一句sql进行自动分页
        List<Notice> notices = noticeMapper.selectForList(empId, qo, admin); //里面不需要自己写limit
        return new PageInfo<Notice>(notices);

    }

    @Override
    public Notice changeReadStatus(Long nid) {
        Long empId = UserContext.getCurrentUser().getId();
        boolean readStatus = noticeMapper.selectReadStatus(nid, empId);


        if (!readStatus) {
            noticeMapper.updateReadStatus(nid, empId, !readStatus);
        }

        return null;
    }

    @Override
    public void updateStatus(Long id) {
        Notice notice = noticeMapper.selectByPrimaryKey(id);
        Boolean status = notice.getStatus();
        noticeMapper.updateStatus(id, !status);
    }

    @Override
    public List<Notice> queryUnreadNotices(Long id) {
        List<Notice> notices = noticeMapper.queryUnreadNotices(id);
        return notices;
    }

    @Override
    public int queryUnreadSize(Long empId) {
        int size = noticeMapper.queryUnreadSize(empId);
        return size;
    }

}
